using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows.Input;


namespace MoneyManagerNET.Presentation.ViewModel
{
    using Model;
    using MoneyManagerNET.Presentation.ViewModel.Base;
using MoneyManagerNET.Presentation.Infrastructure;
    

    public class MainWindowViewModel:ViewModelBase
    {
        public MainWindowViewModel()
        {
            _bankAccounts = new ObservableCollection<IBankAccount>(
                App.Storage.Select<IBankAccount>()
                );

            _stockAccounts = new ObservableCollection<IStockAccount>(
                App.Storage.Select<IStockAccount>().ToList()
                );


            RelocateByCategoryCommand = new Infrastructure.DelegateCommand(o=>RelocateByCategory());
            RelocateByContargentCommand = new Infrastructure.DelegateCommand(o=>RelocateByContargent());

            _manageContragentCommand = new DelegateCommand(ManageContragent);
            _createAccountCommand = new DelegateCommand(CreateAccount);
            _manageCategoriesCommand = new DelegateCommand(ManageCategories);
        }

        ObservableCollection<IBankAccount> _bankAccounts;
        public INotifyCollectionChanged BankAccounts {
            get
            {
                return _bankAccounts;
            } 
        }

        ObservableCollection<IStockAccount> _stockAccounts;
        public INotifyCollectionChanged StockAccounts
        {
            get
            {
                return _stockAccounts;
            }
        }

        int _selectedIndex;
        public int SelectedIndex { get { return _selectedIndex; } set { _selectedIndex = value; OnChange("SelectedIndex"); } }

        string[] tabs = new string[] { "Home","BankAccount","StockAccount" ,"Asset","","","Reports"};
        ViewModelBase[] viewModels = new ViewModelBase[] { new HomeViewModel(), new BankAccountViewModel(), new StockAccountViewModel(), new AssetListViewModel(), null, null, new ReportShellViewModel() };

        public ViewModelBase[] TabViewModels
        {
            get { return viewModels; }
            set { viewModels = value; OnChange("TabViewModels"); }
        }

        public void SelectNode(object nodeValue,string type) 
        {
            int idx=Array.IndexOf(tabs,type);
            SelectedIndex = idx;
            
            var viewModel=TabViewModels[idx];
            if (viewModel!=null)
                viewModel.Model = nodeValue;
        }

        public Node<ReportViewModelBase>[] Reports { 
            get { 
                var reportVM = this.viewModels[Array.IndexOf(tabs, "Reports")] as ReportShellViewModel; 
                return reportVM.ReportGroups; 
            } 
        }


        //public class CurrentSelection
        //{
        //    public object Item { get; set; }
        //    public string Category { get; set; }
        //}


        //CurrentSelection _Selected;
        //public CurrentSelection Selected
        //{
        //    get { return _Selected; }
        //    set { _Selected = value; if (Selected != null)SelectNode(Selected.Item,Selected.Category); }
        //}


        DelegateCommand _createAccountCommand;
        public ICommand CreateAccountCommand { get { return _createAccountCommand; } }

        public void CreateAccount(object o)
        {
            var vm = new CreateAccountViewModel();
            var r=vm.ShowDialog();
            if(r.HasValue&&r.Value){
                var account=vm.CreateAccount();
                
                var vm2 = new EditAccountViewModel() { Model=account};
                var r2=vm2.ShowDialog();
                if (r2.HasValue && r2.Value)
                {
                    if(account is IBankAccount)
                        _bankAccounts.Add(vm2.ModelAs as IBankAccount);

                    if (account is IStockAccount)
                        _stockAccounts.Add(vm2.ModelAs as IStockAccount);

                    App.Storage.SaveAll();
                }
            }
        }

        DelegateCommand _manageContragentCommand ;
        public ICommand ManageContragentCommand
        {get { return _manageContragentCommand; }}

        public void ManageContragent(object o)
        {
            ContragentListViewModel vm = new ContragentListViewModel();
            vm.ShowDialog();
            App.Storage.SaveAll();
        }


        DelegateCommand _manageCategoriesCommand;
        public ICommand ManageCategoriesCommand { get { return _manageCategoriesCommand; } }
        public void ManageCategories(object o)
        {
            CategoryListViewModel vm = new CategoryListViewModel();
            vm.ShowDialog();
            App.Storage.SaveAll();
        }

        public ICommand RelocateByCategoryCommand { get; set; }
        public void RelocateByCategory() {
            var vm = new RelocateCategoryViewModel();
            var r = vm.ShowDialog();
            if (r.HasValue && r.Value)
            {
                App.Service.RelocateByCategory(vm.FromCategory, vm.FromSubCategory, vm.ToCategory, vm.ToSubCategory, null);
                App.Storage.SaveAll();
            }
        }

        public ICommand RelocateByContargentCommand { get; set; }
        public void RelocateByContargent()
        {
            var vm = new RelocateContragentViewModel();
            var r = vm.ShowDialog();
            if (r.HasValue && r.Value)
            {
                App.Service.RelocateByContragent(vm.FromContragent, vm.ToContragent, null);
                App.Storage.SaveAll();

            }
        }

        ~MainWindowViewModel() {
            App.Storage.SaveAll();
        }
    }
}
